/*
* ============================================================================
* 功能描述 ：应用层配置
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/

#include "main.h"
#include <stdio.h>

static int _temp_lcd(int d)
{
	switch(d)
		{
		   case 1:
			   return 11;
			   break;
		   case 2:
			   return 189;
			   break;
		   case 3:
			   return 159;
			   break;
		   case 4:
			   return 79;
			   break;
		   case 5:
			   return 215;

			   break;
		   case 6:
			   return 247;

			   break;
		   case 7:
			   return 139;

			   break;
		   case 8:
			   return 255;

			   break;
		   case 9:
			   return 223;

			   break;
		   case 0:
			   return 251;

			   break;
		   default:
			   break;

		}

}


static void _temp_handle(signed short t, signed short h)
{
	unsigned char t10 = 0, t100 = 0, t1000 = 0;
	unsigned char h10 = 0, h100 = 0, h1000 = 0;
	long  j,j1,j2;

	t10 	= (t/10%10);		// 十位位
	t100 	= (t/100%10);		// 百位
	t1000 	= (t/1000);			// 千位
	DelayXms(2);
//	t_10 	= _temp_lcd(t10);
//	t_100 	= _temp_lcd(t100);
//	t_1000 	= _temp_lcd(t1000);

	j1  = SHORT_GET_HIGH_BYTE(h);
	j2  = SHORT_GET_LOW_BYTE(h);
	
	j  = j1*256+j2;

	h10		= (j/100%10); 		// bai位
	h100	= (j/1000%10);		// qian位	
	h1000	= (j/10000%10);		// wan位
	DelayXms(2);
//	h_10	= _temp_lcd(h10);
//	h_100	= _temp_lcd(h100);
//	h_1000	= _temp_lcd(h1000);

//	hp[0] = SHORT_GET_HIGH_BYTE(h_10);
//	hp[1] = SHORT_GET_LOW_BYTE(h_10);
//	hp[2] = SHORT_GET_HIGH_BYTE(h_100);
//	hp[3] = SHORT_GET_LOW_BYTE(h_100);
//	hp[4] = SHORT_GET_HIGH_BYTE(h_1000);
//	hp[5] = SHORT_GET_LOW_BYTE(h_1000);
	if(t1000 == 1)
	{
		if(t100 <= 8 )
		{
			if(h1000 == 4 || h1000 == 5)
			{
				Write_Lcd_Storage(face_state,1);
			}
			else if(h1000 == 6)
			{
				if(h100 < 5)
				{
					Write_Lcd_Storage(face_state,1);
				}
				else
					Write_Lcd_Storage(face_state,0);
					
			}
			else
				Write_Lcd_Storage(face_state,0);
		}
		else
			Write_Lcd_Storage(face_state,0);
	}
	else if(t1000 == 2)
	{
		if(t100 <= 8)
		{
			if(h1000 == 4 || h1000 == 5)
			{
				Write_Lcd_Storage(face_state,1);
			}
			else if(h1000 == 6)
			{
				if(h100 < 5)
				{
					Write_Lcd_Storage(face_state,1);
				}
				else
					Write_Lcd_Storage(face_state,0);
					
			}
			else
				Write_Lcd_Storage(face_state,0);
		}
		else
			Write_Lcd_Storage(face_state,0);
	}
	else
		Write_Lcd_Storage(face_state,0);
	
#if 0
	char a1[2],a2[2],a3[2];
	a1[0] = h10;
	a2[0] = h100;
	a3[0] = h1000;
	DelayXms(200);
	Uart_Tx_buf(a1, 1);		// 打印每一位值
	DelayXms(200);
	Uart_Tx_buf(a2, 1);
	DelayXms(200);
	Uart_Tx_buf(a3, 1);
#endif
	//Lcd_Set_temp(t_1000, t_100, t_10);
	Write_Lcd_Storage(temp_1,t10);
	DelayXms(2);
	Write_Lcd_Storage(temp_10,t100);
	DelayXms(2);
	Write_Lcd_Storage(temp_100,t1000);
	DelayXms(2);
	Write_Lcd_Storage(hum_10,h100);
	DelayXms(2);
	Write_Lcd_Storage(hum_100,h1000);
	Seg_Set_Data();


}

void Hly_Temp_Hum_Obtain(void)
{	
	unsigned char Data_f;
	long  j,j1,j2;
	char i;
	//unsigned char temp[2], hum[2];
	signed short t,h;
	for(i = 1; i < 10; i++)
	{
		SHT4XReadTemperatureAndHumidity(&t, &h);
	}
	Data_f = SHT4XReadTemperatureAndHumidity(&t, &h);	// 获取温湿度
	j1  = SHORT_GET_HIGH_BYTE(h);
	j2  = SHORT_GET_LOW_BYTE(h);
	
	
	j  = (j1*256+j2)/10;
	if(Data_f == 1)
	{
		Hly_zigbee_up_temp(t);
		DelayXms(2);
		Hly_zigbee_up_hum(j);
		DelayXms(2);
		_temp_handle(t, h);	 // 获取温湿度的每一位值
		//temp[0]  =  SHORT_GET_HIGH_BYTE(t);
		//temp[1]  =  SHORT_GET_LOW_BYTE(t);

		//hum[0]	=  SHORT_GET_HIGH_BYTE(h);
		//hum[1]	=  SHORT_GET_LOW_BYTE(h);
		
		//DelayXms(200);
		//Uart_Tx_buf(temp,2);
	//	Uart_Tx_buf(hum,2);
		
	}	

}


void app_init(void)
{

	Zigbee_Cmd_handle();
	Zigbee_Config_Set();

}

